#include "Agent.h"
#include "../tile/TileManager.h"
#include "../Game.h"
#include "../CreatePunchMessage.h"
#include "../SGD Wrappers/SGD_AudioManager.h"

CAgent::CAgent()
{
}


CAgent::~CAgent()
{
}

void CAgent::Update(float elapsedTime)
{
	//Tile Collision
	CTileManager::GetInstance()->AgentCollision(this->GetRect(), this);

	//Damage Over Time
	if (m_fDOTTime > 0.0f)
	{
		m_fCurrStamina -= m_fDamageOverTime * elapsedTime;
		m_fDOTTime -= elapsedTime;
	}

	//Damage Over Time Expires
	if (m_fDOTTime <= 0.0f)
		m_fDOTTime = m_fDamageOverTime = 0.0f;

	//Slow Effect
	if (m_fSlowTime > 0.0f)
	{
		m_vtVelocity *= m_fSlowEffect;
		m_fSlowTime -= elapsedTime;
	}

	//Slow Effect Expires
	if (m_fSlowTime <= 0.0f)
		m_fSlowTime = m_fSlowTime = 0.0f;

	// Become exhausted when stamina is finished
	if (m_fCurrStamina / m_fMaxStamina <= 0)
		m_bExhausted = true;
	else if (m_fCurrStamina / m_fMaxStamina >= 0.2f)
		m_bExhausted = false;

	m_fTime += elapsedTime;

	Entity::Update(elapsedTime);
}


/*virtual*/ void CAgent::HandleCollision(const Entity* pOther)	/*override*/
{
	if ((pOther->GetType() == ENT_WALL || pOther->GetType() == ENT_PLAYER) && GetType() != ENT_ALLY)
	{
		SGD::Rectangle rIntersect = pOther->GetRect().ComputeIntersection(GetRect());
		if (rIntersect.ComputeHeight() <= rIntersect.ComputeWidth())
		{
			if (GetRect().top == rIntersect.top)
				m_ptPosition.y = pOther->GetPosition().y + pOther->GetSize().height;
			else
				m_ptPosition.y = pOther->GetPosition().y - m_szSize.height;
		}
		else if (rIntersect.ComputeHeight() >= rIntersect.ComputeWidth())
		{
			if (GetRect().left == rIntersect.left)
				m_ptPosition.x = pOther->GetPosition().x + pOther->GetSize().width;
			else
				m_ptPosition.x = pOther->GetPosition().x - m_szSize.width;
		}		
	}
}

void CAgent::Punch()
{
	if (m_fTime > 0.5f)
	{
		CreatePunchMessage* pMsg = new CreatePunchMessage(this);
		pMsg->QueueMessage();
		pMsg = nullptr;

		SGD::AudioManager::GetInstance()->PlayAudio(Game::GetInstance()->GetPunchmissSound());

		m_fTime = 0.0f;
	}
}